import { cookie, addEvent, removeEvent } from './utils'
import audioTabText from './constans'
import Audio from './Audio';

const TiaoSe = {
    init(core) {
        const { namespace } = core.config
        this.size = cookie.get('tiaose',namespace) || ""
        this.ignore = ['LINK','SCRIPT']
        this.namespace = namespace
		//this.body =  document.body
        this.set()
       
    },
    setEvents(core) {
       const { namespace } = core.config
       document.getElementById(`${namespace}-tiaose`).onclick = () => {
		   
        this.tiaose(core) 
       }
       addEvent(window,'DOMContentLoaded',()=>{
           this.updateTiaoseState(core)
       })
    },
    updateTiaoseState(core) {
        let { message } = core
        message.publish('tiaoseState',this.size)
    },
    tiaose(core) {
		         var arr =[{
		                name: 'yellow',
		                color: "black"
		            }, 
					{
					    name: 'blue',
					    color: "white"
					}, 
					{
					    name: 'red',
					    color: "black"
					}] 
	
				//const found = arr.find(elem => elem.name === this.size);
				let mysize=this.size 
				const found = arr.findIndex(function(val){
					return val.name ==mysize 
				})
				if(found<2)
				{
			    this.size = arr[found+1].name;	 
	            }
			   else
			   {
				   this.size=""
				   
			   }
			    console.log(found+1,found)

			
			
			
       // this.size = parseFloat((this.size+0.1).toFixed(10));  
        this.updateTiaoseState(core)
        this.set(); 
        Audio.playAudio(audioTabText.tiaose)
    },
    set() { 
			 document.body.style.backgroundColor = this.size;
		 if(this.size=="black")
		 {
			      // console.log("black")
			 
			      [].forEach.call(document.body.children, (el)=> {
			          const __el = el.tagName.toUpperCase()
			          if(this.ignore.indexOf(__el) > -1 || el.id == this.namespace) {
			              return
			          }
			 			
			         el.style.color ="white!important"
			          //el.style.transformOrigin = '0px 0px'
			      });
			 
		 }
	
		 
		 console.log("tiaose", document.body.style)

        cookie.set('tiaose', this.size, this.namespace)
    },
    reset() {
        this.size = ""
        this.set()
    }   
        
};

export default TiaoSe;